var host = $('#host').val();
var uploadUrl = $('#uploadUrl').val();
var delExcelUrl = $('#delExcelUrl').val();
var uploadFileName = '';
var delObj = {};

$(function () {
    $('.file-caption .file-caption-name').prop('placeholder', '请选择一个excel文件...');
    //$('#continue_upload').show();

    //点击删除导入数据失败中的文件
    $('.importDebtorList').on('click', '.closeImg', function () {
        var obj = $(this);
        delObj = obj;
        var fileName = $(this).data('filename');

        $('.model_upload_excel').val(fileName);

        //$("#delete_excel_modal").trigger("click");

        $.ajax({
            url: delExcelUrl,
            type: 'POST',
            async: true,
            data: {fileName: fileName},
            //timeout: 5000,    //超时时间
            dataType: 'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            // beforeSend: function (xhr) {
            //     console.log(xhr)
            // },
            success: function (data, textStatus, jqXHR) {
                if (data.code == 200) {
                        layer.msg(data.message);
                    obj.parent().remove();

                    $('.defeatedWaring').each(function () {
                        var name = $(this).data('filename');
                        if (name == fileName) {
                            $(this).remove();
                        }
                    })

                } else if (data.code == 400) {
                    layer.msg(data.message);
                }
            },
            error: function (xhr, textStatus) {
                layer.msg('系统错误');
            },
            // complete: function () {
            //
            // }
        })

    })

    $('.package_upload_excel').on('click', '#modal-confirm', function () {
        var obj = delObj;
        var fileName = $(this).parent().find('.model_upload_excel').val();
        $.ajax({
            url: delExcelUrl,
            type: 'POST',
            async: true,
            data: {fileName: fileName},
            //timeout: 5000,    //超时时间
            dataType: 'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            // beforeSend: function (xhr) {
            //     console.log(xhr)
            // },
            success: function (data, textStatus, jqXHR) {
                if (data.code == 200) {
                    layer.msg(data.message);
                    obj.parent().remove();

                    $('.defeatedWaring').each(function () {
                        var name = $(this).data('filename');
                        if (name == fileName) {
                            $(this).remove();
                        }
                    })

                } else if (data.code == 400) {
                    layer.msg(data.message);
                }
            },
            error: function (xhr, textStatus) {
                layer.msg('系统错误');
            },
            // complete: function () {
            //
            // }
        })
    })

});


/*$('#package_file').fileinput({
 showCaption: false,
 previewFileType: 'xlsx',
 browseClass: '',
 browseLabel: '',
 browseIcon: '',
 removeClass: 'btn btn-danger',
 removeLabel: '删除',
 removeIcon: '<i class="glyphicon glyphicon-trash"></i>',
 uploadclass: 'btn btn-info',
 uploadLabel: '上传',
 uploadIcon: '<i class="glyphicon glyphicon-upload"></i>',
 });*/


$("#input-20").fileinput({
    language: 'zh', //设置语言
    uploadUrl: uploadUrl,
    uploadExtraData: {"package_id": $('#package_id').val()},
    //allowedFileExtensions: ['xls', 'xlsx'],
    browseClass: "btn btn-primary btn-block",
    showCaption: false,
    showRemove: false,
    showUpload: false
}).on('fileselect', function (event, numFiles, label) {
    $('#upload_excel').hide();
    //$('#continue_upload').show();
    $('#input-20').fileinput('upload');

}).on("filebatchselected", function (event, files) {
    if (files.length) {
        uploadFileName = files['0']['name'];
    }

}).on('filepreupload', function (event, data, previewId, index) {
    var html = '<li class="pull-left excel_uploading">'
        + '<img src="' + host + 'image/officeHall/uploadAssetPackage/uploading.png" title="' + uploadFileName + '" alt="' + uploadFileName + '" />'
        + '<p>' + uploadFileName + '</p></li>';

    var html2 = '<p class="defeatedUploading"> <span>' + uploadFileName + '</span> <span style="color: #F8FA00;">上传中</span> </p>';

    $('#myupload').before(html);
    $('.promptOne').before(html2);

}).on("fileuploaded", function (event, data, previewId, index) {
    $('.importDebtorList').find('.defeatedUploading').remove();

    var param = data.response;
    var olderFileName = param.data.res.oldFileName;
    if (param.code == 200) {
        $('.importDebtorList ').find('.excel_uploading').remove();

        var html = '<li class="pull-left ">'
            + '<img src="' + host + 'image/officeHall/uploadAssetPackage/closImg.png" ' + 'data-filename="' + param.data.res.fileName + '"' + 'class="closeImg" alt="点击删除"  title="点击删除"/>'
            + '<img src="' + host + 'image/officeHall/uploadAssetPackage/uploadSuccessfully.png" title="' + olderFileName + '" alt="' + olderFileName + '" />'
            + '<p>' + olderFileName + '</p></li>';

        var html2 = '<p class="defaultSuccess"> <span>' + uploadFileName + '</span> <span style="color: green;">上传成功：' + param.message + '</span> </p>';

        $('#upload_access').val('allow');
        $('#myupload').before(html);
        $('.promptOne').before(html2);

    } else if (param.code == 400) {
        $('.importDebtorList ').find('.excel_uploading').remove();
        if (param.data.res != '') {
            var html = '<li class="pull-left errorImg"><img src="' + host + 'image/officeHall/uploadAssetPackage/closImg.png" ' + 'data-filename="' + param.data.res.fileName + '"' + 'class="closeImg" alt="点击删除"  title="点击删除"/> ' +
                '<p>' + olderFileName + '</p > </li>';
            var html2 = '<p class="defeatedWaring" data-filename="' + param.data.res.fileName + '"> <span>' + olderFileName + '</span> <span>上传失败：' + param.message + '</span> </p>';

            $('#myupload').before(html);
        } else {
            var html2 = '<p class="defeatedWaring"> <span>' + uploadFileName + '</span> <span>上传失败：' + param.message + '</span> </p>';
        }

        $('.promptOne').before(html2);
    } else {
        layer.msg('请选择Excel文件');
    }
    //$('.kv-upload-progress').hide();
    //$('.kv-hidden').hide();
}).on('fileerror', function (event, data, msg) {
    $('.importDebtorList').find('.defeatedUploading').remove();
    layer.msg('请选择Excel文件');
});

//上传模板excel
/*$("#input-20").fileinput({
 language: 'zh', //设置语言
 uploadUrl: uploadUrl,
 uploadExtraData: {"package_id": $('#package_id').val()},
 allowedFileExtensions: ['xls', 'xlsx'],
 uploadAsync: true, //默认异步上传
 showUpload: true, //是否显示上传按钮
 showRemove: true, //显示移除按钮
 showClose: true, //显示关闭按钮
 //showPreview :true, //是否显示预览   与input属性 ata-overwrite-initial有关，false不显示,默认true
 showCaption: true,//是否显示标题
 browseClass: "btn btn-primary", //按钮样式
 //browseClass: "btn btn-primary btn-block", //按钮样式
 dropZoneEnabled: false,//是否显示拖拽区域
 //minImageWidth: 50, //图片的最小宽度
 //minImageHeight: 50,//图片的最小高度
 //maxImageWidth: 1000,//图片的最大宽度
 //maxImageHeight: 1000,//图片的最大高度
 //maxFileSize:0,//单位为kb，如果为0表示不限制文件大小
 //minFileCount: 0,
 autoReplace: true, //替换当前文件
 maxFileCount: 1, //表示允许同时上传的最大文件个数
 enctype: 'multipart/form-data',
 validateInitialCount: true,
 previewFileIcon: "<iclass='glyphicon glyphicon-king'></i>",
 msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
 }).on("fileuploaded", function (event, data, previewId, index) {
 var param = data.response;
 var olderFileName = param.data.res.oldFileName;
 if (param.code == 200) {
 var html = '<li class="pull-left ">'
 + '<img src="' + host + 'image/officeHall/uploadAssetPackage/uploadSuccessfully.png" title="' + olderFileName + '" alt="' + olderFileName + '" />'
 + '<p>' + olderFileName + '</p></li>';

 //var html2 = '<p class="defeatedWaring"> <span>' + olderFileName + '</span> <span>上传成功</span> </p>';
 $('#upload_access').val('allow');

 layer.msg('上传成功');
 } else if (param.code == 400) {

 layer.msg('上传失败，失败原因：' + param.message);
 if (param.data.res != '') {
 var html = '<li class="pull-left errorImg"><img src="' + host +
 'image/officeHall/uploadAssetPackage/closImg.png" ' +
 'data-filename="' + param.data.res.fileName + '"' +
 'class="closeImg" alt="点击关闭"  title="点击关闭"/> ' +
 '<p>' + olderFileName + '</p > </li>';
 var html2 = '<p class="defeatedWaring" data-filename="' + param.data.res.fileName + '"> <span>' + olderFileName + '</span> <span>上传失败:' + param.message + '</span> </p>';
 }
 }

 $('#myupload').before(html);
 $('.promptOne').before(html2);
 $('.kv-upload-progress').hide();
 $('.kv-hidden').hide();
 }).on('fileerror', function (event, data, msg) {
 layer.msg('请选择Excel文件');
 });*/
